package dao;

import model.SignUp;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 报名数据访问对象
 */
public class SignUpDAO {
    // 添加报名记录
    public void addSignUp(SignUp signUp) {
        String sql = "INSERT INTO sign_ups (user_id, activity_id, sign_up_time) " +
                "VALUES (?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, signUp.getUserId());
            pstmt.setString(2, signUp.getActivityId());
            pstmt.setTimestamp(3, signUp.getSignUpTime());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据ID查询报名记录
    public SignUp findSignUpById(int id) {
        String sql = "SELECT * FROM sign_ups WHERE id = ?";
        SignUp signUp = null;

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                signUp = new SignUp();
                signUp.setId(rs.getInt("id"));
                signUp.setUserId(rs.getString("user_id"));
                signUp.setActivityId(rs.getString("activity_id"));
                signUp.setSignUpTime(rs.getTimestamp("sign_up_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return signUp;
    }

    // 查询所有报名记录
    public List<SignUp> findAllSignUps() {
        String sql = "SELECT * FROM sign_ups";
        List<SignUp> signUps = new ArrayList<>();

        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                SignUp signUp = new SignUp();
                signUp.setId(rs.getInt("id"));
                signUp.setUserId(rs.getString("user_id"));
                signUp.setActivityId(rs.getString("activity_id"));
                signUp.setSignUpTime(rs.getTimestamp("sign_up_time"));
                signUps.add(signUp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return signUps;
    }

    // 根据用户ID查询报名记录
    public List<SignUp> findSignUpsByUserId(String userId) {
        String sql = "SELECT * FROM sign_ups WHERE user_id = ?";
        List<SignUp> signUps = new ArrayList<>();

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, userId);
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                SignUp signUp = new SignUp();
                signUp.setId(rs.getInt("id"));
                signUp.setUserId(rs.getString("user_id"));
                signUp.setActivityId(rs.getString("activity_id"));
                signUp.setSignUpTime(rs.getTimestamp("sign_up_time"));
                signUps.add(signUp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return signUps;
    }

    // 删除报名记录
    public void deleteSignUp(String userId, String activityId) {
        String sql = "DELETE FROM sign_ups WHERE user_id = ? AND activity_id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, userId);
            pstmt.setString(2, activityId);

            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}